--1.假设刘备取款6000，(添加check约束，设置账户余额必须>=0)，要求：使用事务实现，修改余额和添加取款记录两步操作使用事务
begin transaction 
declare @MyError int = 0
update BankCard set CardMoney = CardMoney-6000 where CardNo = '420107198905064135'
set @MyError = @MyError + @@ERROR
insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime)
values('420107198905064135',0,6000,GETDATE())
set @MyError = @MyError + @@ERROR
if @MyError=0
begin 
	commit transaction
	print '取款成功'
end
	else
begin
	rollback transaction
	print '取款失败，余额不足'
end
;
--2.刘备向张飞转账1000元，(添加check约束，设置账户余额必须>=0)
begin transaction
declare @Error int=0
update BankCard set CardMoney = CardMoney-1000 where CardNo = '420107198905064135'
set @Error = @@ERROR + @Error
update BankCard set CardMoney = CardMoney+1000 where CardNo = '420107199602034138'
set @Error = @@ERROR + @Error
insert into CardTransfer(CardNoOut,CardNoIn,TransferMoney,TransferTime)
values ('420107198905064135','420107199602034138',1000,GETDATE())
set @Error = @@ERROR + @Error
if @Error = 0
	begin 
		commit
		print'转账成功'
	end
else
	begin 
		rollback
		print'转账失败'
	end	
;